package singleton;

public class Client
{
	public static void main(String[] args) {
		System.out.println("=== 测试懒汉式单例 ===");
		testLazySingleton();

		System.out.println("\n=== 测试饿汉式单例 ===");
		testEagerSingleton();

		System.out.println("\n=== 测试枚举单例 ===");
		testEnumSingleton();

		System.out.println("\n=== 测试静态内部类单例 ===");
		testHolderSingleton();
	}

	private static void testLazySingleton() {
		PrintSpoolerSingleton ps1 = PrintSpoolerSingleton.getInstance();
		PrintSpoolerSingleton ps2 = PrintSpoolerSingleton.getInstance();

		System.out.println("ps1 == ps2: " + (ps1 == ps2));  // true

		ps1.manageJobs();
		System.out.println("--------------------------");
		ps2.manageJobs();
	}

	private static void testEagerSingleton() {
		PrintSpoolerSingletonEager ps1 = PrintSpoolerSingletonEager.getInstance();
		PrintSpoolerSingletonEager ps2 = PrintSpoolerSingletonEager.getInstance();

		System.out.println("ps1 == ps2: " + (ps1 == ps2));  // true

		ps1.manageJobs();
		System.out.println("--------------------------");
		ps2.manageJobs();
	}

	private static void testEnumSingleton() {
		PrintSpoolerEnum ps1 = PrintSpoolerEnum.INSTANCE;
		PrintSpoolerEnum ps2 = PrintSpoolerEnum.INSTANCE;

		System.out.println("ps1 == ps2: " + (ps1 == ps2));  // true

		ps1.manageJobs();
		System.out.println("--------------------------");
		ps2.manageJobs();
	}

	private static void testHolderSingleton() {
		PrintSpoolerHolder ps1 = PrintSpoolerHolder.getInstance();
		PrintSpoolerHolder ps2 = PrintSpoolerHolder.getInstance();

		System.out.println("ps1 == ps2: " + (ps1 == ps2));  // true

		ps1.manageJobs();
		System.out.println("--------------------------");
		ps2.manageJobs();
	}
}

